Skip to main content

Enum type

If there are only a few possible values for a data type and each value has its own meaning, to improve the readability of the code, they can be defined as Enum types, which are called enumerations in Chinese.

enum colors {RED, GREEN, BLUE};

printf("%d\n", RED); // 0
printf("%d\n", GREEN); // 1
printf("%d\n", BLUE); // 2

In the above example, assuming that three colours are needed inside the program, you can use the enum command to define these three colours as an enumeration type colors, which has only three possible values RED, GREEN and BLUE. At this point, these three names automatically become integer constants, and the compiler sets their values to the numbers 0, 1 and 2 by default. By contrast, RED is much more readable than 0.

Note that the constant names inside Enum, obey the naming convention for identifiers, but are usually in upper case.

When used, variables can be declared as Enum types.

enum colors color;

The above code declares the variable color as type enum colors. The value of this variable is one of the constants RED, GREEN and BLUE.

color = BLUE;
printf("%i\n", color); // 2

The above code sets the value of the variable color to BLUE, where BLUE is a constant with a value equal to 2.

The typedef command can be used to alias the Enum type.

typedef enum {
SHEEP,
WHEAT,
WOOD,
BRICK,
ORE
} RESOURCE;

RESOURCE r;

In the above example, RESOURCE is an alias for the Enum type. It is sufficient to use this alias when declaring variables.

A less common way of writing this is to declare the Enum type and assign a value to the variable on the same line.

enum {
SHEEP,
WHEAT,
WOOD,
BRICK,
ORE
} r = BRICK, s = WOOD;

In the example above, the value of r is 3 and the value of s is 2.

Since the properties of an Enum are automatically declared as constants, sometimes the purpose of using an Enum is not to customise a data type, but to declare a set of constants. This is when it is easier to use a writeup like the following.

enum { ONE, TWO };

printf("%d %d", ONE, TWO); // 0 1

In the example above, enum is a keyword followed by a block of code, and the constants are declared right inside the code. ONE and TWO are the two Enum constants.

The constants are separated by commas. The trailing comma after the last constant can be omitted or retained.

enum { ONE, TWO, };

Since Enum is automatically numbered, it is not necessary to assign a value to a constant; C will automatically increment the value of a constant from 0 onwards. However, C also allows values to be specified for ENUM constants, although they can only be specified as integers, not other types. Therefore, an Enum constant can be used in any situation where an integer can be used.

enum { ONE = 1, TWO = 2 }

printf("%d %d", ONE, TWO); // 1 2

Enum constants can be non-contiguous values.

enum { X = 2, Y = 18, Z = -2 };

Enum constants can also be the same value.

enum { X = 2, Y = 2, Z = 2 };

If some of a set of constants have a value specified and some do not. Then the unspecified constants are automatically assigned incrementally from the last specified constant.

enum {
A, // 0
B, // 1
C = 4, // 4
D, // 5
E, // 6
F = 3 // 3
G, // 4
H // 5
}

The scope of an Enum is the same as that of a variable. If declared at the top level, it is valid throughout the file; if declared inside a block of code, it is valid only for that block. Enum has the benefit of being a clearer representation of code intent if compared to constants declared with int.